<template>
    <div class="e-form">
        <BillTop title="采购验收新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.handoverPurchase" :rules="rules" label-width="160px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="验收日期：" prop="acceptanceDate">
                            <el-date-picker
                                v-model="formData.handoverPurchase.acceptanceDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="验收人：">
                            <span>{{ formData.handoverPurchase.founderName }}</span>
                        </el-form-item>
                    </el-col>
                     <el-col :span="12">
                        <el-form-item label="购置日期：" prop="purchaseDate">
                            <el-date-picker
                                v-model="formData.handoverPurchase.purchaseDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                         <el-form-item label="合同名称：" prop="contractName">
                            <el-input v-model="formData.handoverPurchase.contractName" readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click.stop="selectContract"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="供应单位：">
                            <span>{{ formData.handoverPurchase.supplierName }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="验收地点：" prop="acceptanceSiteDetails">
                            <SelectAddr v-model="formData.handoverPurchase.acceptanceSiteDetails"
                                lazy
                                showCountry
                            ></SelectAddr>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                   <el-col :span="12">
                         <el-form-item label="管理单位：" prop="managementUnit">
                            <el-input v-model="formData.handoverPurchase.managementUnit"  readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectUnit"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                          <span>{{ statusLable(formData.handoverPurchase.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                     <el-col :span="24">
                        <el-form-item label="机构：">
                            <span>{{userInfo.orgInfo.orgName}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item  label="备注：">
                        <el-input type="textarea" v-model="formData.handoverPurchase.remarks" placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose"  v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/purchaseAcceptance.js'
import { mapState, mapMutations } from 'vuex'
import SelectAddr from '@/components/common/addrComp'
import { selectChildrenOrg, selectPurchaseContract } from '@/api/equip/popupCallback.js'
export default {
    data () {
        return {
            formData: {
                //采购验收基础信息
                handoverPurchase: {
                    'acceptanceDate': '',
                    'acceptanceSite': '',
                    'acceptanceSiteDetails': '',
                    'acceptanceSiteList': [],
                    'acceptanceSiteNumber': '',
                    'acceptanceSiteNumberList': [],
                    'auditDate': '',
                    'auditor': '',
                    'auditorId': '',
                    'billDate': '',
                    'billNo': '',
                    'contractId': '',
                    'contractName': '',
                    'contractNo': '',
                    'gmtCreate': '',
                    'gmtModified': '',
                    'managementId': '',
                    'managementUnit': '',
                    'modifier': '',
                    'modifierId': '',
                    'orgId': '',
                    'orgName': '',
                    'purchaseDate': '',
                    'purchaseId': '',
                    'founderName': '',
                    'founderId': '',
                    'remarks': '',
                    'state': 0,
                    'supplierId': '',
                    'supplierName': '',
                    'workId': ''
                },
            },
            rules: {
                acceptanceDate: [
                    { required: true, message: '请选择验收日期', trigger: 'change' },
                ],
                purchaseDate: [
                    { required: true, message: '请选择购置日期', trigger: 'change'  },
                ],
                contractName: [
                    { required: true, message: '请选择合同名称', trigger: 'change' },
                ],
                acceptanceSiteDetails: [
                    { required: true, message: '请填写验收地点', trigger: 'blur' },
                ],
                managementUnit: [
                    { required: true, message: '请选择管理单位', trigger: 'change' },
                ],
            },
            tableData: [],
        }
    },
    components: {
        SelectAddr
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            workStatus: state=>state.equip.equipData.workStatus,
            assessmentLevel: state=>state.equip.equipData.assessmentLevel,
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return obj ? obj.label : ''
            }
        },
    },
    created () {
        const date = new Date()
        this.formData.handoverPurchase.acceptanceDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
        this.formData.handoverPurchase.founderName = localStorage.getItem('userName')
    },
    mounted () {
        this.userId = localStorage.getItem('userId')
    },
    methods: {
        ...mapMutations(['setSelectedInfo']),
        //选择设备采购合同
        selectContract () {
            let ids = this.formData.handoverPurchase.contractId ? [this.formData.handoverPurchase.contractId] : []
            selectPurchaseContract({ 'orgId': this.userInfo.orgInfo.orgId, 'ids': ids }).then(res=>{
                res.forEach(item=>{
                    this.formData.handoverPurchase.contractId = item.contractId //合同id
                    this.formData.handoverPurchase.contractNo = item.contractNumber //合同编号
                    this.formData.handoverPurchase.contractName = item.contractName //合同名称
                    this.formData.handoverPurchase.supplierName = item.secondPartyName
                    this.formData.handoverPurchase.supplierId = item.secondPartyId
                })
            })
        },
        //选择管理单位(本机构及下级机构)
        selectUnit () {
            selectChildrenOrg().then(res=>{
                this.formData.handoverPurchase.managementId = res[0].orgId
                this.formData.handoverPurchase.managementUnit = res[0].orgName
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if(valid) {
                    request.purchaseAcceptanceAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/purchaseAcceptanceEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                        // this.tableData.forEach(item=>{
                        //     item.billId = this.billid
                        // })
                        // request.updatePurchaseAcceptanceDel(this.tableData).then(()=>{

                        // })
                    })
                }else{
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/purchaseAcceptanceList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
